package com.samoiloff.ui.list.smart.controller {
	import flash.events.Event;
	import com.samoiloff.ui.list.smart.interfaces.CellRenderer;
	import com.samoiloff.ui.list.smart.model.SmartListModel;
	import com.samoiloff.framework.controller.AbstractController;

	/**
	 * @author Samoiloff
	 */
	public class SmartListSelectedController extends AbstractController {
		
		private var _vModel: SmartListModel;
				
		private var _selected: CellRenderer;
		
		public function SmartListSelectedController(model: SmartListModel) {
			super(model);
			
			_vModel = model;
			
			_vModel.addEventListener(SmartListModel.SELECTED_INDEX_CHANGED, onSelectedIndexChanged);
		}

		private function onSelectedIndexChanged(event: Event): void {
			if (_selected) {
				_selected.selected = false;
				_selected = null;
			}
			
			if (_vModel.selectedIndex >= 0) {
				_selected = _vModel.view.renderers.getItemAt(_vModel.selectedIndex);
				_selected.selected = true; 
			}
		}
	}
}
